package cn.iocoder.yudao.module.student.controller.app.user;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.alibaba.IdAuthenticationUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi;
import cn.iocoder.yudao.module.bpm.api.task.dto.BpmFormSimpleRespDTO;
import cn.iocoder.yudao.module.member.api.user.dto.RealNameReqDTO;
import cn.iocoder.yudao.module.student.controller.app.student.vo.WebStudentUpdatePhoneReqVO;
import cn.iocoder.yudao.module.student.controller.app.student.vo.WebStudentUpdateReqVO;
import cn.iocoder.yudao.module.student.dal.dataobject.student.WebStudentDO;
import cn.iocoder.yudao.module.student.service.student.WebStudentService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.SystemGroupCountDTO;
import cn.iocoder.yudao.module.system.api.user.dto.UserCreateReqDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 -跳过权限- 用户")
@RestController
@RequestMapping("/public/user")
@Validated
public class WebUserController {

    @Resource
    private AdminUserApi adminUserApi;

    @Resource
    private BpmProcessInstanceApi bpmProcessInstanceApi;
    @Resource
    private WebStudentService webStudentService;
    @PostMapping("/createCompanyUser")
    @ApiOperation("创建企业用户")
//    @PreAuthorize("@ss.hasPermission('student:web-student:create')")
    public CommonResult<Long> createCompanyUser(@Valid @RequestBody UserCreateReqDTO createReqVO) {
        return success(adminUserApi.createCompanyUser(createReqVO));
    }

    @PostMapping("/list-all-simple")
    @ApiOperation("获取数据字典")
    public CommonResult<List<BpmFormSimpleRespDTO>> getSimpleForms(@Valid @RequestBody UserCreateReqDTO createReqVO) {
        return success( bpmProcessInstanceApi.getSimpleForms());
    }

    @PostMapping("/isRealName")
    @ApiOperation("三要素认证")
    @TenantIgnore
    @PreAuthenticated
    public CommonResult< Map<String,Object>> isRealName(@RequestBody RealNameReqDTO realNameReqDTO) {
        Map<String,Object> flag= IdAuthenticationUtils.idAuthentication(realNameReqDTO.getCardNo(),realNameReqDTO.getPhone(),realNameReqDTO.getUserName());
        WebStudentDO webStudent=webStudentService.getWebStudent(realNameReqDTO.getPhone());
        if(webStudent!=null) {
            if(!flag.isEmpty()){
                if(flag.get("code").equals("1")){
                    webStudent.setIsRealName("yes");
                }else {
                    webStudent.setIsRealName("no");
                }
            }
            WebStudentUpdateReqVO updateReqVO = new WebStudentUpdateReqVO();
            updateReqVO.setPostDate(new Date());
            updateReqVO.setId(webStudent.getId());
            updateReqVO.setIsRealName(webStudent.getIsRealName());
            webStudentService.updateIsRealName(updateReqVO);
        }
        return success(flag);
    }
    @GetMapping("/group/count")
    @ApiOperation("按模块统计访问量")
    @TenantIgnore
    @PreAuthenticated
    public CommonResult<List<SystemGroupCountDTO>> getOperateLogGroupCount(String startDate, String endDate) {
        List<SystemGroupCountDTO> oList = adminUserApi.getOperateLogGroupCont(startDate, endDate);
        return success(oList);
    }


}
